/*******************************************************************************
Name: create_census_cells
*******************************************************************************/


clear

capture log close

log using  "${logdir}2-create_census_cells.log", replace


/*----------------------------------------------------------------------*/
/*----------------------------------------------------------------------*/
*COLLAPSE IPUMS CENSUS DATA INTO CELLS (BY COHORT, EDUCATION, RACE, SEX)
/*----------------------------------------------------------------------*/
/*----------------------------------------------------------------------*/

forvalues year = 1970(10)2000 {
	
use "${rawdir}ipums_`year'.dta", clear


drop if metarea == 0
keep if age >= 16 & age <= 64
keep if (race == 1 | race == 2) & hispan == 0

keep if gq == 1

gen cohort_cat = 0

if `year' == 1970 {
	replace cohort_cat = 4 if age >= 24 & age < 34
	replace cohort_cat = 3 if age >= 34 & age < 44
	replace cohort_cat = 2 if age >= 44 & age < 54
	replace cohort_cat = 1 if age >= 54 & age <= 64
}

if `year' == 1980 {
	replace cohort_cat = 5 if age >= 24 & age < 34
	replace cohort_cat = 4 if age >= 34 & age < 44
	replace cohort_cat = 3 if age >= 44 & age < 54
	replace cohort_cat = 2 if age >= 54 & age <= 64
}

if `year' == 1990 {
	replace cohort_cat = 6 if age >= 24 & age < 34
	replace cohort_cat = 5 if age >= 34 & age < 44
	replace cohort_cat = 4 if age >= 44 & age < 54
	replace cohort_cat = 3 if age >= 54 & age <= 64
}

if `year' == 2000 {
	replace cohort_cat = 7 if age >= 24 & age < 34
	replace cohort_cat = 6 if age >= 34 & age < 44
	replace cohort_cat = 5 if age >= 44 & age < 54
	replace cohort_cat = 4 if age >= 54 & age <= 64
}

keep if cohort_cat > 0

gen educ_mod = 0
replace educ_mod = 0 if educd == 0
replace educ_mod = 0 if educd == 1
replace educ_mod = 0 if educd == 2 
replace educ_mod = 2 if educd == 10 
replace educ_mod = 0 if educd == 11 
replace educ_mod = 0 if educd == 12
replace educ_mod = 2.5 if educd == 13
replace educ_mod = 1 if educd == 14
replace educ_mod = 2 if educd == 15
replace educ_mod = 3 if educd == 16
replace educ_mod = 4 if educd == 17
replace educ_mod = 6.5 if educd == 20
replace educ_mod = 5.5 if educd == 21
replace educ_mod = 5 if educd == 22
replace educ_mod = 6 if educd == 23
replace educ_mod = 7.5 if educd == 24
replace educ_mod = 7 if educd == 25
replace educ_mod = 8 if educd == 26
replace educ_mod = 9 if educd == 30
replace educ_mod = 10 if educd == 40
replace educ_mod = 11 if educd == 50
replace educ_mod = 12 if educd == 60
replace educ_mod = 11.5 if educd == 61
replace educ_mod = 12 if educd == 62
replace educ_mod = 12.5 if educd == 65
replace educ_mod = 13 if educd == 70
replace educ_mod = 13 if educd == 71
replace educ_mod = 14 if educd == 80
replace educ_mod = 14 if educd == 81
replace educ_mod = 14 if educd == 82
replace educ_mod = 14 if educd == 83
replace educ_mod = 15 if educd == 90
replace educ_mod = 16 if educd == 100
replace educ_mod = 16 if educd == 101
replace educ_mod = 16 if educd == 110
replace educ_mod = 16 if educd == 111
replace educ_mod = 16 if educd == 112
replace educ_mod = 16 if educd == 113
replace educ_mod = 16 if educd == 114
replace educ_mod = 16 if educd == 115
replace educ_mod = 16 if educd == 116

gen educ_cat = .
replace educ_cat = 1 if educ_mod < 12
replace educ_cat = 2 if educ_mod >= 12 & educ_mod < 16
replace educ_cat = 3 if educ_mod == 16

drop if educ_cat == .

*census regions
rename region division
gen region = floor(division/10)

replace division = 21 if metarea == 164
replace region = 2 if metarea == 164

replace division = 31 if metarea == 340
replace region = 3 if metarea == 340

replace division = 32 if metarea == 452
replace region = 3 if metarea == 452

replace division = 31 if metarea == 916
replace region = 3 if metarea == 916

drop if region == 9

gen age_cat = 0
replace age_cat = 1 if age >= 24 & age < 29
replace age_cat = 2 if age >= 29 & age < 34
replace age_cat = 3 if age >= 34 & age < 39
replace age_cat = 4 if age >= 39 & age < 44
replace age_cat = 5 if age >= 44 & age < 49
replace age_cat = 6 if age >= 49 & age < 54
replace age_cat = 7 if age >= 54 & age < 59
replace age_cat = 8 if age >= 59 & age <= 64


gen employed = (empstat == 1)

if `year' == 1970 {
	gen inschool = gradeatt > 0 & gradeatt != .
} 
else {
	gen inschool = (school == 2)
}

gen idle = !employed & !inschool

drop age_cat

gen age_cat = 0
replace age_cat = 4 if age >= 24 & age < 34
replace age_cat = 3 if age >= 34 & age < 44
replace age_cat = 2 if age >= 44 & age < 54
replace age_cat = 1 if age >= 54 & age <= 64

egen count = count(year), by(year metarea age_cat cohort_cat educ_cat race sex)

collapse (mean) employed inschool idle incwage count [aweight = perwt], by(year metarea age_cat cohort_cat educ_cat race sex)

if `year' == 1970 {
	save "${datadir}ls_70.dta", replace
}

if `year' == 1980 {
	save "${datadir}ls_80.dta", replace
}

if `year' == 1990 {
	save "${datadir}ls_90.dta", replace
}

if `year' == 2000 {
	save "${datadir}ls_00.dta", replace
}

}

/*----------------------------------------------------------------------*/
*1970 5-YEAR QUASI-PANEL
/*----------------------------------------------------------------------*/

use "${rawdir}ipums_1970.dta", clear

drop if metarea == 0
keep if age >= 16 & age <= 64
keep if (race == 1 | race == 2) & hispan == 0

keep if gq == 1

gen cohort_cat = 0
replace cohort_cat = 4 if age >= 24 & age < 34
replace cohort_cat = 3 if age >= 34 & age < 44
replace cohort_cat = 2 if age >= 44 & age < 54
replace cohort_cat = 1 if age >= 54 & age <= 64

keep if cohort_cat > 0

gen educ_mod = 0
replace educ_mod = 0 if educd == 2 
replace educ_mod = 0 if educd == 11 
replace educ_mod = 0 if educd == 12
replace educ_mod = 1 if educd == 14
replace educ_mod = 2 if educd == 15
replace educ_mod = 3 if educd == 16
replace educ_mod = 4 if educd == 17
replace educ_mod = 5 if educd == 22
replace educ_mod = 6 if educd == 23
replace educ_mod = 7 if educd == 25
replace educ_mod = 8 if educd == 26
replace educ_mod = 9 if educd == 30
replace educ_mod = 10 if educd == 40
replace educ_mod = 11 if educd == 50
replace educ_mod = 12 if educd == 60
replace educ_mod = 12.5 if educd == 65
replace educ_mod = 13 if educd == 70
replace educ_mod = 14 if educd == 80
replace educ_mod = 15 if educd == 90
replace educ_mod = 16 if educd == 100
replace educ_mod = 16 if educd == 110
replace educ_mod = 16 if educd == 111
replace educ_mod = 16 if educd == 112
replace educ_mod = 16 if educd == 113

gen educ_cat = .
replace educ_cat = 1 if educ_mod < 12
replace educ_cat = 2 if educ_mod >= 12 & educ_mod < 16
replace educ_cat = 3 if educ_mod == 16

drop if educ_cat == .

*census regions
rename region division
gen region = floor(division/10)

replace division = 21 if metarea == 164
replace region = 2 if metarea == 164

replace division = 31 if metarea == 340
replace region = 3 if metarea == 340

replace division = 32 if metarea == 452
replace region = 3 if metarea == 452

replace division = 31 if metarea == 916
replace region = 3 if metarea == 916

drop if region == 9

gen employed = (empstat == 1)
gen inschool = gradeatt > 0 & gradeatt != .
gen idle = !employed & !inschool

gen idle5yr = (coll5yr == 1 & work5yr == 1)

egen count_ls = count(year), by(metarea race sex cohort_cat educ_cat)
egen weight_cell = sum(perwt), by(year metarea race sex cohort_cat educ_cat)

gen active = 1 - idle
gen active5yr = 1 - idle5yr


collapse active active5yr weight_cell count_ls [aw = perwt], by(year metarea race sex cohort_cat educ_cat)

gen active_fd5yr = active - active5yr
gen ln_active_fd5yr = ln(active) - ln(active5yr)


save "${datadir}ls_70_panel.dta", replace


/*----------------------------------------------------------------------*/
*1980 5-YEAR QUASI-PANEL
/*----------------------------------------------------------------------*/

use "${rawdir}ipums_1980.dta", clear

drop if metarea == 0
keep if age >= 16 & age <= 64
keep if (race == 1 | race == 2) & hispan == 0

keep if gq == 1

gen cohort_cat = 0
replace cohort_cat = 5 if age >= 24 & age < 34
replace cohort_cat = 4 if age >= 34 & age < 44
replace cohort_cat = 3 if age >= 44 & age < 54
replace cohort_cat = 2 if age >= 54 & age <= 64

keep if cohort_cat > 0

gen educ_mod = 0
replace educ_mod = 0 if educd == 2 
replace educ_mod = 0 if educd == 11 
replace educ_mod = 0 if educd == 12
replace educ_mod = 1 if educd == 14
replace educ_mod = 2 if educd == 15
replace educ_mod = 3 if educd == 16
replace educ_mod = 4 if educd == 17
replace educ_mod = 5 if educd == 22
replace educ_mod = 6 if educd == 23
replace educ_mod = 7 if educd == 25
replace educ_mod = 8 if educd == 26
replace educ_mod = 9 if educd == 30
replace educ_mod = 10 if educd == 40
replace educ_mod = 11 if educd == 50
replace educ_mod = 12 if educd == 60
replace educ_mod = 12.5 if educd == 65
replace educ_mod = 13 if educd == 70
replace educ_mod = 14 if educd == 80
replace educ_mod = 15 if educd == 90
replace educ_mod = 16 if educd == 100
replace educ_mod = 16 if educd == 110
replace educ_mod = 16 if educd == 111
replace educ_mod = 16 if educd == 112
replace educ_mod = 16 if educd == 113

gen educ_cat = .
replace educ_cat = 1 if educ_mod < 12
replace educ_cat = 2 if educ_mod >= 12 & educ_mod < 16
replace educ_cat = 3 if educ_mod == 16

drop if educ_cat == .

*census regions
rename region division
gen region = floor(division/10)

replace division = 21 if metarea == 164
replace region = 2 if metarea == 164

replace division = 31 if metarea == 340
replace region = 3 if metarea == 340

replace division = 32 if metarea == 452
replace region = 3 if metarea == 452

replace division = 31 if metarea == 916
replace region = 3 if metarea == 916

drop if region == 9

gen employed = (empstat == 1)
gen inschool = gradeatt > 0 & gradeatt != .
gen idle = !employed & !inschool

save "${datadir}ls_80_panel.dta", replace

log close

